package com.wzh.loveEmail.service.Impl;

import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.wzh.loveEmail.bean.WeatherInfo;
import com.wzh.loveEmail.config.TianApiConfig;
import com.wzh.loveEmail.constants.UrlConstant;
import com.wzh.loveEmail.exception.ServiceException;
import com.wzh.loveEmail.form.ApiBaseResult;
import com.wzh.loveEmail.form.ApiCommonResult;
import com.wzh.loveEmail.form.ApiListCommonResult;
import com.wzh.loveEmail.service.ApiService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author wzh
 * @date 2024/12/10
 * @apiNote
 */
@Slf4j
@Service
public class ApiServiceImpl implements ApiService {

    @Resource
    private TianApiConfig tianApiConfig;

    @Override
    public String getCaiHongPiInfo() {
        String requestUrl = UrlConstant.CAI_HONG_API + tianApiConfig.getAppKey();
        String getString = getBody(requestUrl);
        ApiListCommonResult result = JSONUtil.toBean(getString, ApiListCommonResult.class);
        String content = result.getNewslist().get(0).getContent();
        if (content.toLowerCase().contains("xxx")) {
            content.replace("xxx","你").replace("XXX","你");
        }
        return content.replace("他", "你").replace("她", "你");
    }

    @Override
    public String getDogDiary() {
        String requestUrl = UrlConstant.DOG_DIARY_API + tianApiConfig.getAppKey();
        String getString = getBody(requestUrl);
        ApiCommonResult result = JSONUtil.toBean(getString, ApiCommonResult.class);
        return result.getResult().getContent().replace("他", "你").replace("她", "你");
    }


//
//    @Override
//    public JSONArray getReSouInfo() {
//
//        String requestUrl = UrlConstant.RE_SOU_API + tianApiConfig.getAppKey();
//        String getString = HttpUtil.createGet(requestUrl).execute().body();
//        JSONObject obj = JSONUtil.parseObj(getString);
//        log.info("obj"+obj.toString());
//        JSONArray newslist = obj.getJSONArray("newslist");
//        return newslist;
//    }

    @Override
    public String getZaoAnInfo() {
        String requestUrl = UrlConstant.MORNING_API + tianApiConfig.getAppKey();
        String getString = getBody(requestUrl);
        ApiListCommonResult result = JSONUtil.toBean(getString, ApiListCommonResult.class);
        return result.getNewslist().get(0).getContent();
    }

    @Override
    public WeatherInfo getWeatherInfo(String city) {
        String requestUrl = UrlConstant.TIAN_QI_API + tianApiConfig.getAppKey() + "&city=" + city;
        String getString = HttpUtil.createGet(requestUrl).execute().body();
        JSONObject obj = JSONUtil.parseObj(getString);
        JSONArray newslist = obj.getJSONArray("newslist");
        List<WeatherInfo> weatherInfos = JSONUtil.toList(newslist, WeatherInfo.class);
        return weatherInfos.get(0);
    }

    @Override
    public String getJoke() {
        String requestUrl = UrlConstant.JOKE_API + tianApiConfig.getAppKey() + "&num=1";
        String getString = getBody(requestUrl);
        ApiListCommonResult result = JSONUtil.toBean(getString, ApiListCommonResult.class);
        return result.getNewslist().get(0).getContent();
    }

    @Override
    public String getSayLove() {
        String requestUrl = UrlConstant.SAY_LOVE_API + tianApiConfig.getAppKey();
        String getString = getBody(requestUrl);
        ApiListCommonResult result = JSONUtil.toBean(getString, ApiListCommonResult.class);
        return result.getNewslist().get(0).getContent().replace("他", "你").replace("她", "你");
    }


    private static String getBody(String url) {
        String body = HttpUtil.createGet(url)
                .contentType("application/x-www-form-urlencoded;charset=UTF-8")
                .charset("UTF-8")
                .execute()
                .body();
        ApiBaseResult base = JSONUtil.toBean(body, ApiBaseResult.class);
        if (!Integer.valueOf(200).equals(base.getCode())) {
            String errorMsg = "接口:" + url + "报错,msg:" + base.getMsg();
            log.error(errorMsg);
            throw new ServiceException(errorMsg);
        }
        return body;
    }
}
